#include "param.h"
#include "ui_param.h"
#include "database/usersdb.h"
#include "common/common.h"
#include <QApplication>

Param::Param(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Param)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::Window);

    //设置背景图片
    QPalette palette = this->palette();
    palette.setBrush(this->backgroundRole(),QBrush(QPixmap(":/resources/black.jpg")));
    this->setPalette(palette);
    //设置输入框限制
    QRegExp regx("[1-9][0-9]");
    QValidator *validator = new QRegExpValidator(regx,  ui->lineEdit);
    ui->lineEdit->setValidator( validator);

    userdb = new UsersDB();
}

Param::~Param()
{
    delete ui;
}

/**
 * @brief 显示和初始化参数面板
 *
 */
void Param::showParam()
{
    user = userdb->getUserInfo();
    switch (user.difficulty)
    {
    case 0:
        ui->radioButton->setChecked(true);
        break;
    case 1:
        ui->radioButton_2->setChecked(true);
        break;
    case 2:
        ui->radioButton_3->setChecked(true);
        break;
    default:
        break;
    }

    switch (user.touch)
    {
    case 0:
        ui->radioButton_5->setChecked(true);
        break;
    case 1:
        ui->radioButton_4->setChecked(true);
        break;
    default:
        break;
    }

    QString fontsize = QString::number( user.fontsize, 10);
    ui->lineEdit->setText(fontsize);

    //设置事例字体点阵
    QFont font;
    font.setPointSize(user.fontsize);
    ui->label_2->setFont(font);

    this->show();
}

/**
 * @brief 取消按钮
 *
 */
void Param::on_pushButton_3_clicked()
{
    this->hide();
    emit enterBegin();
}

/**
 * @brief 字体参考输入框
 *
 * @param fontsize 字体大小
 */
void Param::on_lineEdit_textChanged(const QString &fontsize)
{
    QFont font;
    if (fontsize.isEmpty())
    {
        font.setPointSize(20);
        ui->label_2->setFont(font);
        ui->label_2->setText("字体不能为空");
        return;
    }
    int fontSize = fontsize.toInt();
    //设置事例字体点阵
    font.setPointSize(fontSize);
    ui->label_2->setFont(font);
    ui->label_2->setText("字体大小");
}

/**
 * @brief 默认设置按钮
 *
 */
void Param::on_pushButton_clicked()
{
    ui->radioButton_2->setChecked(true);
    ui->radioButton_4->setChecked(true);
    ui->lineEdit->setText("24");
}

/**
 * @brief 确定按钮
 *
 */
void Param::on_pushButton_2_clicked()
{
    if (ui->lineEdit->text().isEmpty())
    {
        return;
    }

    if ( ui->radioButton->isChecked())
        user.difficulty = 0;
    else if ( ui->radioButton_2->isChecked())
        user.difficulty = 1;
    else
        user.difficulty = 2;

    if ( ui->radioButton_5->isChecked())
        user.touch = 0;
    else
        user.touch = 1;

    user.fontsize = ui->lineEdit->text().toInt();

    userdb->updateParam(user);

    //设置事例字体点阵
    QFont font;
    font.setPointSize(user.fontsize);
    qApp->setFont(font);
    ui->label->setFont(font);
    ui->groupBox->setFont(font);
    ui->radioButton->setFont(font);
    ui->radioButton_2->setFont(font);
    ui->radioButton_3->setFont(font);

    this->hide();

    emit enterBegin();
}
